package dz.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.List;

public class FileTool {
	public static void main(String[] args) {

		
	}
	
	public static void getFileInThisDir(File dir,List<File> list){
		for(File file : dir.listFiles()){
			if(file.isDirectory()){
				getFileInThisDir(file,list);
			}
			else{
				list.add(file);
			}
		}
	}
	
	
	
	public static String formatName(File file){
		String newname = file.getName();
		newname = newname.replace("}", "")
				.replace("{", "")
				.replace("(", "")
				.replace(")", "")
				.replace("[", "")
				.replace("]", "")
				.replace("`", "")
				.replace("%", "")
				.replace(" ", "")
				.replace(":", "")
				.replace("=", "")
				.replace("?", "")
				.replace("_", "");
		
		if(!newname.equals(file.getName())){
//			System.out.println(file.getName() + "=>" + newname);
		}
		String newpath = file.getParent()+"/" + newname;
		File targetFile = new File(newpath);
		file.renameTo(targetFile);
		
		return newpath;
	}
	
	public static String getMD5(String fileName)
	{
		String hashType= "MD5";
		InputStream fis;
		try {
			fis = new FileInputStream(fileName);
			byte[] buffer = new byte[1024];
			MessageDigest md5 = MessageDigest.getInstance(hashType);
			int numRead = 0;
			while ((numRead = fis.read(buffer)) > 0) {
				md5.update(buffer, 0, numRead);
			}
			fis.close();
			return toHexString(md5.digest());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
			sb.append(hexChar[b[i] & 0x0f]);
		}
		return sb.toString();
	}
	
	

	

	
	public static boolean move(File sourceFile, File targetFile)
	{
		new File(targetFile.getParent()).mkdirs();
		sourceFile.renameTo(targetFile);
		return !sourceFile.exists() && targetFile.exists();
	}



	public static boolean copy(File sourceFile, File targetFile) {
		copy(sourceFile.getAbsoluteFile(),targetFile.getAbsolutePath());
		return targetFile.exists();
	}

	
	
	public static void copy(File oldfile, String newPath)	{    
		File parentFolder = new File(new File(newPath).getParent());
		if(!parentFolder.exists()){
			parentFolder.mkdirs();
		}
		try{    
			int bytesum = 0;    
			int byteread = 0;    
			//File oldfile = new File(oldPath);    
			if (oldfile.exists()) {  
				InputStream inStream = new FileInputStream(oldfile); 
				FileOutputStream fs = new FileOutputStream(newPath);    
				byte[] buffer = new byte[1444];    
				while ( (byteread = inStream.read(buffer)) != -1) {    
					bytesum += byteread;    
//					System.out.println(bytesum);    
					fs.write(buffer, 0, byteread);    
				}    
				inStream.close();    
			}    
		}    
		catch (Exception e) {    
			System.out.println( "error  ");    
			e.printStackTrace();    
		}    
	}
}
